<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Manual\Controller\Admin;
use Admin\Controller\AdminController;
/**
 * 章节控制器
 * @author jry <598821125@qq.com>
 */
class ManualPageController extends AdminController{
    /**
     * 章节列表
     * @author jry <598821125@qq.com>
     */
    public function index($mid){
        //获取所有章节
        $map['mid'] = array('eq', $mid); //章节ID
        $map['status'] = array('egt', '0'); //禁用和正常状态
        $data_list = D('ManualPage')->where($map)->order('id asc')->select();

        //转换成树状列表
        $tree = new \Common\Util\Tree();
        $data_list = $tree->toFormatTree($data_list, 'title', 'id', 'pid', 0, false);

        //使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('章节列表') //设置页面标题
                ->addTopButton('addnew', array('href' => U('add', array('mid' => $mid))))  //添加新增按钮
                ->addTopButton('delete')  //添加删除按钮
                ->addTableColumn('id', 'ID')
                ->addTableColumn('title_show', '标题')
                ->addTableColumn('ctime', '创建时间', 'time')
                ->addTableColumn('status', '状态', 'status')
                ->addTableColumn('right_button', '操作', 'btn')
                ->setTableDataList($data_list) //数据列表
                ->addRightButton('edit')   //添加编辑按钮
                ->addRightButton('forbid', array('model' => 'ManualPage')) //添加禁用/启用按钮
                ->addRightButton('delete', array('model' => 'ManualPage')) //添加删除按钮
                ->setTemplate('Admin/_Builder/listbuilder')
                ->display();
    }
    
    
    /**
     * 新增章节
     * @author jry <598821125@qq.com>
     */
    public function add($mid = 0){
        if(IS_POST){
            $weixin_member_public_object = D('ManualPage');
            $data = $weixin_member_public_object->create();
            if($data){
                $id = $weixin_member_public_object->add();
                if($id){
                    $this->success('新增成功', U('index', array('mid' => $mid)));
                }else{
                    $this->error('新增失败');
                }
            }else{
                $this->error($weixin_member_public_object->getError());
            }
        }else{
            //使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('新增章节') //设置页面标题
                    ->setPostUrl(U('add')) //设置表单提交地址
                    ->addFormItem('mid', 'hidden', 'mid', 'mid')
                    ->addFormItem('pid', 'select', '父章节', '父章节', select_list_as_tree('ManualPage',array('mid'=>$mid)))
                    ->addFormItem('title', 'text', '标题', '章节标题')
                    ->addFormItem('content', 'kindeditor', '正文内容', '正文内容')
                    ->setFormData(array('mid' => $mid))
                    ->setTemplate('Admin/_Builder/formbuilder')
                    ->display();
        }
    }

    /**
     * 编辑章节
     * @author jry <598821125@qq.com>
     */
    public function edit($id, $mid = 0){
        if(IS_POST){
            $weixin_member_public_object = D('ManualPage');
            $data = $weixin_member_public_object->create();
            if($data){
                if($weixin_member_public_object->save()!== false){
                    $this->success('更新成功', U('index', array('mid' => $mid)));
                }else{
                    $this->error('更新失败');
                }
            }else{
                $this->error($weixin_member_public_object->getError());
            }
        }else{
            //使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('编辑章节') //设置页面标题
                    ->setPostUrl(U('edit')) //设置表单提交地址
                    ->addFormItem('id', 'hidden', 'ID', 'ID')
                    ->addFormItem('mid', 'hidden', 'mid', 'mid')
                    ->addFormItem('pid', 'select', '父章节', '父章节', select_list_as_tree('ManualPage'))
                    ->addFormItem('title', 'text', '标题', '章节标题')
                    ->addFormItem('content', 'kindeditor', '正文内容', '正文内容')
                    ->addFormItem('sort', 'num', '排序', '用于显示的顺序')
                    ->setFormData(D('ManualPage')->find($id))
                    ->setTemplate('Admin/_Builder/formbuilder')
                    ->display();
        }
    }
}